package com.example.Dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.callservice.GMap;
import com.example.callservice.R;
import com.example.object.ElectricPanels;
import com.example.ultil.Ultil;

public class DialogPanelList extends Dialog {
	private ArrayAdapter<String> listAdapter;
	GMap activity;
	Context context;
	String textSearch;
	ArrayList<ElectricPanels> lstElectPanel;

	@SuppressWarnings("unchecked")
	public DialogPanelList(Activity activtyGMap, Context context,
			ArrayList<ElectricPanels> lstElecPanel, String text) {
		super(context);
		this.activity = (GMap) activtyGMap;
		// TODO Auto-generated constructor stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_panel_list);
		this.context = context;
		textSearch = text;
		ListView lvPanel = (ListView) findViewById(R.id.lv);
		ListViewPanelAdapter adapter = new ListViewPanelAdapter();
		lvPanel.setAdapter(adapter);
		lvPanel.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				activity.drawMapPanel(lstElectPanel.get(pos), -1);
			}
		});
	}

	public ArrayList<ElectricPanels> getPanelSearch(String txtSeach) {
		ArrayList<ElectricPanels> lstElecPanel = new ArrayList<ElectricPanels>();
		for (int i = 0; i < Ultil.lstElectricPanel.size(); i++) {
			if (Ultil.lstElectricPanel.get(i).getPanel_Name().toUpperCase()
					.contains(txtSeach.toUpperCase())) {
				lstElecPanel.add(Ultil.lstElectricPanel.get(i));
			}
		}
		return lstElecPanel;
	}

	public class ListViewPanelAdapter extends BaseAdapter {

		public ListViewPanelAdapter() {
			lstElectPanel = getPanelSearch(textSearch);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lstElectPanel.size();
		}

		@Override
		public Object getItem(int pos) {
			// TODO Auto-generated method stub
			return lstElectPanel.get(pos);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowView = inflater.inflate(R.layout.listview_item, null);
			TextView textView = (TextView) rowView.findViewById(R.id.txtName);
			textView.setText(lstElectPanel.get(position).getPanel_Name());
			return rowView;
		}
	}
}
